import cv2
import numpy as np
from tqdm import tqdm
import os

imagePath="/disk/lihang/deepLearning/data/shiying/imagenet230425"

#
# img = cv2.imread("F:\\tmp\\imagenet230425\\test\\jtsy\\IMG_20230117_155018.jpg")
# high = img.shape[0]
# width = img.shape[1]
#print(high,width)

def checkDirForDeepIsExists(dir_name):
    if not os.path.exists(dir_name):  # os模块判断并创建
        deep_dir_name = os.path.dirname(dir_name)
        checkDirForDeepIsExists(deep_dir_name)
        os.mkdir(dir_name)

for dirpath, dirnames, filenames in (os.walk(imagePath)):
    for filename in tqdm(filenames):
        if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(".bmp"):

            # 方法一：将图像向右旋转90度
            file1 = os.path.join(dirpath,filename)
            img = cv2.imread(file1)
            high = img.shape[0]
            width = img.shape[1]
            if(high<width):
                # cv2.imshow("normal", img)
                # print('Before rotate image shape is',img.shape)
                # cv2.waitKey(0)
                img90 = np.rot90(img, -1)    # 对图像矩阵顺时针旋转90度
                # cv2.imshow("rotate", img90)
                # print('After rotate image shape is',img90.shape)
                # checkDirForDeepIsExists
                # saveNewPath=(os.path.join(dirpath,filename).replace(imagePath,savePath))
                # checkDirForDeepIsExists(saveNewPath)
                cv2.imwrite(os.path.join(dirpath,filename), img90)  # 保存旋转后的图像
                #cv2.waitKey(0)